package com.medic.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.medic.entity.Group;
import com.medic.service.impl.GroupServiceImpl;


@Controller	
@RequestMapping("/web/group")
public class GroupWebController {
	
	@Resource
	private GroupServiceImpl groupService;
	
		
	@ResponseBody()
	@RequestMapping("action/remove/{group}")
	public Object remove(@PathVariable("group")int group){
		Map<String,Object> object = new HashMap<String, Object>();
		
		try{
			groupService.dismissGroup(group);
			
		}catch(Exception e){
			object.put("status" ,"error");
		}
		
		return object ;
	}	
	
	@ResponseBody
	@RequestMapping("index/{pageno}")
	
	public Object  indexPager(@PathVariable("pageno")int  pageno,Model model){
		
		Map<String,Object> object=new HashMap<String,Object>();
		try{
			
			
			Page<Group> page = groupService.getGroup(pageno);
			 
			object.put("data",page);

			

			int  cur = page.getPageNum();
			object.put("next",cur!=page.getPages() ? cur+1 : -2);  

			
			object.put("pre",cur==1 ? -1 : cur-1);

		}catch(Exception e){
			e.printStackTrace(); 
		}
		
		return object;
	}
	@RequestMapping("index")

	public String  index(Model model){
		
		try{
			

			Page<Group> page = groupService.getGroup(1);
			 
			model.addAttribute("data",page);

			

			int  cur = page.getPageNum();
			
			
			model.addAttribute("next",cur!=page.getPages() ? cur+1 : -2); 

		}catch(Exception e){
			e.printStackTrace(); 
		}
		
		return "/web/GROUP";
	}

	
	
	

}
